home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBIMPORT.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  172 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbimport.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #include <stdio.h>
  14. #ifdef AC_STDLIB
  15. #include <stdlib.h>
  16. #endif
  17. #ifdef AC_STRING
  18. #include <string.h>
  19. #endif
  20.  
  21. /* non-ansi headers */
  22. #include <bool.h>
  23.  
  24. /* local headers */
  25. #include "cbase_.h"
  26.  
  27. /*man---------------------------------------------------------------------------
  28. NAME
  29.      cbimport - cbase import
  30.  
  31. SYNOPSIS
  32.      #include <cbase.h>
  33.  
  34.      int cbimport(cbp, filename)
  35.      cbase_t *cbp;
  36.      const char *filename;
  37.  
  38. DESCRIPTION
  39.      The cbimport function imports all records from a text file to
  40.      cbase cbp.  filename points to a character string that contains
  41.      the name of the text file.  See cbexport for a definition of the
  42.      file format.
  43.  
  44.      If a record containing an illegal duplicate key is encountered
  45.      during the import, that record is skipped and the import
  46.      continues with the subsequent record.  On successful completion
  47.      of the remainder of the import, a value of -1 is returned with
  48.      errno set to CBEDUP.  Whether or not the calling program should
  49.      treat this as an error condition is application dependent.
  50.  
  51.      cbimport will fail if one or more of the following is true:
  52.  
  53.      [EINVAL]       cbp is not a valid cbase pointer.
  54.      [EINVAL]       filename is the NULL pointer.
  55.      [ENOENT]       filename does not exist.
  56.      [CBEDUP]       An illegal duplicate key was encountered.
  57.      [CBELOCK]      cbp is not write-locked.
  58.      [CBEPRFILE]    Error reading from text file.
  59.      [CBEPRFILE]    Invalid text file format.
  60.  
  61. SEE ALSO
  62.      cbexport.
  63.  
  64. DIAGNOSTICS
  65.      Upon successful completion, a value of 0 is returned.  Otherwise,
  66.      a value of -1 is returned, and errno set to indicate the error.
  67.  
  68. ------------------------------------------------------------------------------*/
  69. #ifdef AC_PROTO
  70. int cbimport(cbase_t *cbp, const char *filename)
  71. #else
  72. int cbimport(cbp, filename)
  73. cbase_t *cbp;
  74. const char *filename;
  75. #endif
  76. {
  77.     FILE *    fp    = NULL;        /* import stream */
  78.     int    c    = 0;        /* int character */
  79.     void *    buf    = NULL;        /* input buffer */
  80.     bool    dupflag    = FALSE;    /* illegal duplicate key flag */
  81.     bool    errflag    = FALSE;    /* error flag */
  82.     int    field    = 0;        /* field number */
  83.     size_t    fldos    = 0;        /* field offset */
  84.     size_t    fldlen    = 0;        /* field length */
  85.     int    fldtype    = 0;        /* field data type */
  86.     int    terrno    = 0;        /* tmp errno */
  87.  
  88.     /* validate arguments */
  89.     if (!cb_valid(cbp) || filename == NULL) {
  90.         errno = EINVAL;
  91.         return -1;
  92.     }
  93.  
  94.     /* open file for reading */
  95.     fp = fopen(filename, "r");
  96.     if (fp == NULL) {
  97.         return -1;
  98.     }
  99.  
  100.     /* import all records from text file */
  101.     buf = calloc((size_t)1, cbrecsize(cbp));
  102.     if (buf == NULL) {
  103.         CBEPRINT;
  104.         fclose(fp);
  105.         errno = ENOMEM;
  106.         return -1;
  107.     }
  108.     for (;;) {
  109.         /* check for eof */
  110.         if ((c = getc(fp)) == EOF) {
  111.             if (ferror(fp)) {
  112.                 CBEPRINT;
  113.                 terrno = errno;
  114.                 errflag = TRUE;
  115.                 break;
  116.             }
  117.             if (feof(fp)) break;
  118.         }
  119.         if (ungetc(c, fp) == EOF) {
  120.             CBEPRINT;
  121.             terrno = errno;
  122.             errflag = TRUE;
  123.             break;
  124.         }
  125.         memset(buf, 0, cbrecsize(cbp));
  126.         /* input each field of new record */
  127.         for (field = 0; field < cbp->fldc; ++field) {
  128.             fldos = cbp->fldv[field].offset;
  129.             fldlen = cbp->fldv[field].len;
  130.             fldtype = cbp->fldv[field].type;
  131.             /* read field from text file */
  132.             if ((*cbimpv[fldtype])(fp, (char *)buf + fldos, fldlen) == -1) {
  133.                 CBEPRINT;
  134.                 terrno = CBEPRFILE;
  135.                 errflag = TRUE;
  136.                 break;
  137.             }
  138.         }
  139.         /* add record to database */
  140.         if (cbinsert(cbp, buf) == -1) {
  141.             if (errno == CBEDUP) {
  142.                 dupflag = TRUE;
  143.             } else {
  144.                 terrno = errno;
  145.                 errflag = TRUE;
  146.                 break;
  147.             }
  148.         }
  149.     }
  150.     free(buf);
  151.     buf = NULL;
  152.     if (errflag) {
  153.         fclose(fp);
  154.         errno = terrno;
  155.         return -1;
  156.     }
  157.  
  158.     /* close file */
  159.     if (fclose(fp) == EOF) {
  160.         CBEPRINT;
  161.         return -1;
  162.     }
  163.  
  164.     /* notify if illegal duplicate key */
  165.     if (dupflag) {
  166.         errno = CBEDUP;
  167.         return -1;
  168.     }
  169.  
  170.     return 0;
  171. }
  172.